package com.yanqu.road.dao.impl.activity.yingxin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.yingxin.YingXinUserData;
import com.yanqu.road.entity.activity.yingxin.floor.YingXinFloorData;
import com.yanqu.road.entity.activity.yingxin.game.YingXinGameData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YingXinDataDaoImpl extends TempDataObjectDao<YingXinUserData> {

    public static String getYingXinUserDataSql = "select * from `t_u_activity_yingxin_user` where `activityId`=? and `userId`=?";

    public YingXinUserData getYingXinUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getYingXinUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateYingXinUserDataSql = "update `t_u_activity_yingxin_user` set energy=?,lastRecTime=?,collectList=?,score=?,floorDataMap=?,gameDataMap=?,gotBigRewardMap=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(YingXinUserData yingXinUserData) {
        yingXinUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, yingXinUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getCollectList())));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getFloorDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getGameDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getGotBigRewardMap())));
        para.put(i++, new DbParameter(Types.INTEGER, yingXinUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getUserId()));
        if (getDb().executeNoneQuery(updateYingXinUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertYingXinUserDataSql = "insert into `t_u_activity_yingxin_user`(activityId,userId,energy,lastRecTime,collectList,score,floorDataMap,gameDataMap,gotBigRewardMap) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(YingXinUserData yingXinUserData) {
        yingXinUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, yingXinUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, yingXinUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getCollectList())));
        para.put(i++, new DbParameter(Types.BIGINT, yingXinUserData.getScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getFloorDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getGameDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(yingXinUserData.getGotBigRewardMap())));
        if (getDb().executeNoneQuery(insertYingXinUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected YingXinUserData parseData(ResultSet rs) throws SQLException {
        YingXinUserData yingXinUserData = new YingXinUserData();
        yingXinUserData.setActivityId(rs.getInt("activityId"));
        yingXinUserData.setUserId(rs.getLong("userId"));
        yingXinUserData.setEnergy(rs.getInt("energy"));
        yingXinUserData.setLastRecTime(rs.getLong("lastRecTime"));
        yingXinUserData.setCollectList(JSON.parseObject(rs.getString("collectList"), new TypeReference<ConcurrentHashMap<Integer, List<Integer>>>() {
        }));
        yingXinUserData.setScore(rs.getLong("score"));
        yingXinUserData.setFloorDataMap(JSON.parseObject(rs.getString("floorDataMap"), new TypeReference<ConcurrentHashMap<Integer, YingXinFloorData>>() {
        }));
        yingXinUserData.setGameDataMap(JSON.parseObject(rs.getString("gameDataMap"), new TypeReference<ConcurrentHashMap<Integer, YingXinGameData>>() {
        }));
        String gotBigRewardMap = rs.getString("gotBigRewardMap");
        yingXinUserData.setGotBigRewardMap(StringUtils.isNullOrEmpty(gotBigRewardMap) ? new ConcurrentHashMap<>() : JSON.parseObject(gotBigRewardMap, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        return yingXinUserData;
    }

}
